package com.aitao.util;


import com.aitao.beans.Attribute;
import com.aitao.beans.MarkPoint;
import com.aitao.exception.CmdRunException;

/**
 * 断言工具类
 * 
 * @Company QAX
 * @Author : ai.tao
 * @Create : 2022/2/20 16:10
 */
public class Assert {
    public static String mark(String message, Throwable t) {
        MarkPoint markPoint = new MarkPoint(t.getStackTrace()[0]);
        return markPoint + message;
    }

    public static <T> T checkCmdRunNonNull(T obj, Attribute<String, String> message, Throwable t) {
        if (Validator.isEmpty(obj)) {
            throw new CmdRunException(String.format(message.getKey(), mp(t), message.getValue()));
        }
        return obj;
    }

    public static <T> T checkRuntimeNonNull(T obj, Attribute<String, String> message, Throwable t) {
        if (Validator.isEmpty(obj)) {
            throw new RuntimeException(String.format(message.getKey(), mp(t), message.getValue()));
        }
        return obj;
    }

    public static void checkArrayIndexOutOfBoundsException(int index) {
        if (index < 0) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
    }

    public static void checkArrayIndexOutOfBoundsException(int index, int length) {
        if (index >= length) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
    }

    private static MarkPoint mp(Throwable t) {
        return new MarkPoint(t.getStackTrace()[0]);
    }
}
